package com.traffic.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Date;

import com.traffic.commons.utils.DateUtils;
import com.traffic.commons.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.traffic.commons.base.BaseController;
import com.traffic.commons.utils.PageInfo;
import com.traffic.model.TrafficUser;
import com.traffic.service.ITrafficUserService;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 用户表  前端控制器
 * </p>
 * @author Hongyue Wang
 * @since 2017-04-20
 */
@Controller
@RequestMapping("/trafficUser")
public class TrafficUserController extends BaseController{
    
    @Autowired private ITrafficUserService trafficUserService;
    
    @GetMapping("/manager")
    public String manager() {
        return "admin/trafficUser/trafficUserList";
    }


    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(TrafficUser trafficUser, Integer page, Integer rows, String sort,String order) {
        trafficUser.setIsDelete("N");
        EntityWrapper<TrafficUser> ew = new EntityWrapper<TrafficUser>();
        ew.setEntity(trafficUser);
        Page<TrafficUser> pages = getPage(page, rows, sort, order);
        pages = trafficUserService.selectPage(pages,ew);
        return pageToPageInfo(pages);
    }
    
    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/trafficUser/trafficUserAdd";
    }

    /**
     * 添加
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(TrafficUser trafficUser) {
        trafficUser.setCreatTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficUser.setEditTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficUser.setIsDelete("N");
        boolean b = trafficUserService.insert(trafficUser);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        TrafficUser trafficUser = new TrafficUser();
        trafficUser.setId(id.intValue());
        trafficUser.setEditTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficUser.setIsDelete("Y");
        boolean b = trafficUserService.updateById(trafficUser);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }

    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        TrafficUser trafficUser = trafficUserService.selectById(id);
        model.addAttribute("trafficUser", trafficUser);
        return "admin/trafficUser/trafficUserEdit";
    }

    /**
     * 编辑
     * @param 
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(TrafficUser trafficUser) {
        trafficUser.setEditTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        boolean b = trafficUserService.updateById(trafficUser);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }

    @RequestMapping(value="/export")
    public void export(HttpServletRequest request, HttpServletResponse response) {
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("application/x-excel");
            response.setHeader("Content-Disposition", "attachment;filename="
                    + new String("会员列表.xls".getBytes(), "ISO-8859-1"));
            String[] titles = {"会员姓名", "会员手机号"};
            String[] fields = {"Username", "Mobile"};
            TrafficUser model = new TrafficUser();
            model.setIsDelete("N");
            model.setIsLocked("N");
            EntityWrapper<TrafficUser> ew = new EntityWrapper<TrafficUser>();
            ew.setEntity(model);
            List<TrafficUser> list = trafficUserService.selectList(ew);
            ExcelUtils.exportExcel("会员列表.xls", titles, fields, list, null, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
